package com.ruoyi.system.frontierEvent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.system.frontierEvent.bo.TAttachAddBo;
import com.ruoyi.system.frontierEvent.bo.TAttachQueryBo;
import com.ruoyi.system.frontierEvent.bo.TAttachEditBo;
import com.ruoyi.system.frontierEvent.domain.TAttach;
import com.ruoyi.system.frontierEvent.mapper.TAttachMapper;
import com.ruoyi.system.frontierEvent.vo.TAttachVo;
import com.ruoyi.system.frontierEvent.service.ITAttachService;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 附件Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-19
 */
@Service
public class TAttachServiceImpl extends ServiceImpl<TAttachMapper, TAttach> implements ITAttachService {

    @Override
    public TAttachVo queryById(Long id){
        TAttach db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TAttachVo.class);
    }

    @Override
    public List<TAttachVo> queryList(TAttachQueryBo bo) {
        LambdaQueryWrapper<TAttach> lqw = Wrappers.lambdaQuery();
        return entity2Vo(this.list(lqw));
    }

    /**
     * 根据案件Id查询附件列表
     */
    @Override
    public List<TAttachVo> queryCaseAttachList(Long caseId, Long activityid) {
        LambdaQueryWrapper<TAttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(caseId>0, TAttach::getEvtId,caseId);
        lqw.eq(activityid>0, TAttach::getEvtWfId,activityid);
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<TAttachVo> entity2Vo(Collection<TAttach> collection) {
        List<TAttachVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TAttachVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TAttach> page = (Page<TAttach>)collection;
            Page<TAttachVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    public List<TAttachVo> selectByShipId(Long shipId) {

        return this.baseMapper.selectByShipId(shipId);
    }

    @Override
    public Boolean insertByAddBo(TAttachAddBo bo) {
        TAttach add = BeanUtil.toBean(bo, TAttach.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TAttachEditBo bo) {
        TAttach update = BeanUtil.toBean(bo, TAttach.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TAttach entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public Boolean deleteByFileId(String fileId) {
        return this.baseMapper.deleteByFileId(fileId)>0;
    }
}
